package copyengine.resource.resProxy.cacheData
{
    import copyengine.log.CELog;
    import copyengine.utils.GeneralUtils;

    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;

    public class CESwfResCacheData extends CEResCacheDataBasic
    {
        private var mLoader:Loader;
        private var mDomain:ApplicationDomain;

        public function CESwfResCacheData()
        {
            super();
        }

        override protected function doLoadRes():void
        {
            mLoader = new Loader()
            addListener();
            mLoader.load(new URLRequest(mFileDesc.filePath));
        }

        override public function getData():* {return mDomain;}

        //========================//
        //==Private  Function
        //=========================//

        private function addListener():void
        {
            GeneralUtils.addTargetEventListener(mLoader.contentLoaderInfo, Event.COMPLETE, onLoaded);
            GeneralUtils.addTargetEventListener(mLoader.contentLoaderInfo, ProgressEvent.PROGRESS, onLoadInProgress);
            GeneralUtils.addTargetEventListener(mLoader.contentLoaderInfo, IOErrorEvent.IO_ERROR, onLoadInError);
        }

        private function removeListener():void
        {
            GeneralUtils.removeTargetEventListener(mLoader.contentLoaderInfo, Event.COMPLETE, onLoaded);
            GeneralUtils.removeTargetEventListener(mLoader.contentLoaderInfo, ProgressEvent.PROGRESS, onLoadInProgress);
            GeneralUtils.removeTargetEventListener(mLoader.contentLoaderInfo, IOErrorEvent.IO_ERROR, onLoadInError);
        }

        private function onLoaded(e:Event):void
        {
            var loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
            mDomain = loaderInfo.applicationDomain;
            removeListener();
            onResLoadComplete();
        }

        private function onLoadInProgress(e:ProgressEvent):void
        {
        }

        private function onLoadInError(e:IOErrorEvent):void
        {
            removeListener();
            CELog.err(e.toString());
            CELog.err("CESwfResCacheData-> load file in error, file: " + mFileDesc.toString());
        }

    }
}
